<?
/**
 *账单记录类.
 *冲数据库获得记录详细信息，调用detail()可查询详细订单.
 */
class BillRecord{

    /**
     *票付通内部接口对象.
     *@var object
     */
    private $s;
    
    /**
     *账单记录表ID.
     *@var object
     */
    private $id;
    
    /**
     *键值数组，账单详细.
     *@var array
     */
    private $b_info;
    
    /**
     *账单包含订单simplexml对象.
     *@var object
     */
    private $simplexml;
    
    /**
     *所包含订单统计,该统计是实时统计，账单记录信息是生成账单时记录的,是调用内部口直接回答
     *格式array(0=>订单数,1=>门票数,2=>总金额).
     *@var array
     *@uses BillRecord::detail()
     */
    private $totals;

    private $payType;
    private $billFlag;
    /**
     *构造函数.
     *@param int $id 账单记录表ID
     *@param SoapClient $s 票付通内部接口对象
     *@param int $flag 账单类型 0默认所有订单1线下支付/在线支付
     *@param string $payType 账单记录表ID
     */
    public function __construct($id,$s,$flag = 0, $payType = '0|1|2'){
        $this->id           = $id;
        $this->s            = $s;
        $this->payType      = $payType;
        $this->billFlag     = $flag;
        $this->billTable    = !$flag ? 'uu_bill' : 'uu_bill_offline';
    }

    /**
     *账单记录详细函数.
     *返回含账单记录详细的键值数组
     *@return  array
     */
    public function info(){
        if(!$this->b_info){
            $str=<<<SQL
select b.id,b.fid,b.aid,b.status,b.btime,b.etime,b.payoff/*,m1.dname as fname,
m2.dname as aname*/ FROM {$this->billTable} b/*
LEFT JOIN pft_member m1 ON b.fid=m1.id
LEFT JOIN pft_member m2 on b.aid=m2.id */WHERE b.id={$this->id} LIMIT 1;
SQL;
            $GLOBALS['le']->query($str);
            $this->b_info=$GLOBALS['le']->fetch_assoc();
        }
        return $this->b_info;
    }
    
    /**
     *账单记录详细函数.
     *返回含账单记录详细的键值数组
     *@return  array
     */
    public function detail($lid="",$tid=""){
        if(!$this->simplexml){
            $b_info=$this->info();
            $params=array(
                "btime3"=>$b_info['btime'],
                "etime3"=>$b_info['etime'],
                "lid"=>$lid,
                "tid"=>$tid,
                "status"=>"1|6",
                "pays"=> $this->payType,
                "orderby"=>1,
                "sort"=>1,
                "rstart"=>0,
                "n"=>9999999,
                "c"=>0,
                "stype"=>2,
                "payinfo"=>2,
                "pmode"=>0,
                "remotenum"=>"",
                "origin"=>0,
                "p_type"=>"",
                "order_confirm"=>"",
                "concat"=>0,
                "aid"=>$b_info['aid'],
                "mid"=>$b_info['fid'],
                "ifpack"=>0);
            extract($params);
            
            $count = $this->s->Order_Globle_Search($sid,$mid,$lid,$tid,$ltitle,$ttitle,$btime1,$etime1,$btime2,$etime2,$btime3,$etime3,$ordernum,$oname,$otel,$status,$pays,$fromt,$orderby,$sort,$rstart,$n,2,$stype,$payinfo,$pmode,$remotenum,$origin,$p_type, $order_confirm,$aid,$concat,$ifpack);
            $count=explode(",",$count);
            $this->totals=$count;
        
            $xml=$this->s->Order_Globle_Search($sid,$mid,$lid,$tid,$ltitle,$ttitle,$btime1,$etime1,$btime2,$etime2,$btime3,$etime3,$ordernum,$oname,$otel,$status,$pays,$fromt,$orderby,$sort,$rstart,$count[0],0,$stype,$payinfo,$pmode,$remotenum,$origin,$p_type, $order_confirm,$aid,$concat,$ifpack);
            $this->simplexml=simplexml_load_string($xml);
        }
        
        return $this->simplexml;
    }
    
    
    /**
     *账单记录详细函数.
     *返回含账单记录详细的键值数组
     *@return  array
     *解决账单明细显示的问题，问题在注释
     * 如果账单跟数量对不上，可能是联票的关系
     */
    public function _detail($lid="",$tid=""){
        if(!$this->simplexml){
            $b_info=$this->info();
            $params=array(
                "btime3"    =>$b_info['btime'],
                "etime3"    =>$b_info['etime'],
                "lid"       =>$lid,
                "tid"       =>$tid,
                "status"    =>"",
                "pays"      => "{$this->payType}",
                "orderby"   =>'3',
                "sort"      =>'1',
                "rstart"    =>0,
                "n"     =>9999999,
                "c"     =>0,
                "stype" =>0,
    // 查询分类（0全部    1电商接口部分2分销平台部分3全部但会员ID仅作用分销部分), 
                "stype" =>0,
                // "payinfo"=>"2", 
    // 余额支付（为空不做筛选1使用个人余额|2使用分销余额|3信用支付), 
                "payinfo"=>"",
                "pmode"=>0,
                "remotenum"=>"",
                "origin"=>0,
                "p_type"=>"",
                "order_confirm"=>"",
                "concat"=>0,
                "aid"=>$b_info['aid'],
                "mid"=>$b_info['fid'],
                "ifpack"=>0
            );
            extract($params);
            $count=$this->s->Order_Globle_Search($sid,$mid,$lid,$tid,$ltitle,$ttitle,$btime1,$etime1,$btime2,$etime2,$btime3,$etime3,$ordernum,$oname,$otel,$status,$pays,$fromt,$orderby,$sort,$rstart,$n,2,$stype,$payinfo,$pmode,$remotenum,$origin,$p_type, $order_confirm,$aid,$concat,$ifpack);

//            $this->simplexml=$simplexml;
            $count=explode(",",$count);
            $this->totals=$count;
        
            $xml=$this->s->Order_Globle_Search($sid,$mid,$lid,$tid,$ltitle,$ttitle,$btime1,$etime1,$btime2,$etime2,$btime3,$etime3,$ordernum,$oname,$otel,$status,$pays,$fromt,$orderby,$sort,$rstart,$count[0],0,$stype,$payinfo,$pmode,$remotenum,$origin,$p_type, $order_confirm,$aid,$concat,$ifpack);
            $this->simplexml=simplexml_load_string($xml);
        }
        $newXML = array();
        foreach ($this->simplexml->Rec as $v) {
            $this->secondhand($v);
            if((int)$v->UUnosx != 1){
                $newXML[] = $v;
            }
        }

        return $newXML;
    }
    
    /**
     *账单记录订单汇总统计函数.
     *由传入函数按景区或门票统计订单
     *@param string $countby 账户汇总，默认按景区汇总
     *@param array 
     */
    public function ordercount($countby="lid"){
        $data=$this->detail();
        $count=array();
        foreach($data as $row){
            if($countby=="lid"){
                $count[(string)$row->UUlid]['ltitle']=$row->UUltitle;
                $count[(string)$row->UUlid]['tnum']+=$row->UUtnum;
                $count[(string)$row->UUlid]['order']++;
                $count[(string)$row->UUlid]['money']+=(string)$row->UUtotalmoney;
                $count[(string)$row->UUlid]['countby']="lid";
                $count[(string)$row->UUlid]['salerid']=(string)$row->UUsalerid;
            }else{
                $count[(string)$row->UUtid]['ltitle']=$row->UUltitle.$row->UUttitle;
                $count[(string)$row->UUtid]['tnum']+=$row->UUtnum;
                $count[(string)$row->UUtid]['order']++;
                $count[(string)$row->UUtid]['money']+=(string)$row->UUtotalmoney;
                $count[(string)$row->UUtid]['countby']="pid";
                $count[(string)$row->UUtid]['salerid']=(string)$row->UUsalerid;
            }
        }
        return $count;
    }
	//改一下
	public function _ordercount($countby="lid",$r){
        $data=$r;
        $count=array();
        foreach($data as $row){
            if($countby=="lid"){
                $count[(string)$row->UUlid]['ltitle']=$row->UUltitle;
                $count[(string)$row->UUlid]['tnum']+=$row->UUtnum;
                $count[(string)$row->UUlid]['order']++;
                $count[(string)$row->UUlid]['money']+=$row->UUtprice*(int)$row->UUtnum;
                $count[(string)$row->UUlid]['countby']="lid";
                $count[(string)$row->UUlid]['salerid']=(string)$row->UUsalerid;
            }else{
                $count[(string)$row->UUtid]['ltitle']=$row->UUltitle.$row->UUttitle;
                $count[(string)$row->UUtid]['tnum']+=$row->UUtnum;
                $count[(string)$row->UUtid]['order']++;
                $count[(string)$row->UUtid]['money']+=$row->UUtprice*(int)$row->UUtnum;
                $count[(string)$row->UUtid]['countby']="pid";
                $count[(string)$row->UUtid]['salerid']=(string)$row->UUsalerid;
            }
        }
        return $count;
    }
    
    /**
     *返回账单所包含的订单的实时统计.
     *@return  array
     */
    public function billcount(){
        if(!$this->totals){
            $this->detail();
        }
        return $this->totals;
    }

    public function secondhand($row){
        $row->UUnosx = 0;//no-授信
        if ((string)$row->UUaids !='0' && (string)$row->UUaids_price !='0') { //是多级供应产品
            //排除不是授信支付方式的
            //当是多级转分销时 要看UUaids_money
            $aid_money = unserialize((string)$row->UUaids_money);
            $UUaids = explode(",", (string)$row->UUaids);
            $UUaids_price = explode(",", (string)$row->UUaids_price);
            if($this->b_info['aid']==$_SESSION['sid']){//应收
                $position = array_search($_SESSION['sid'],$UUaids);
                if($position == (count($UUaids)-1)){//最末级供应商
                    if((int)$row->UUpmode != 2){
                        $row->UUnosx = 1;//不是授信的标记
                    }
                    return $row;
                }
                foreach($aid_money as $v){
                    if($v[0]==$UUaids[$position+1]){
                        if($v[1] == 1) //余额
                        {
                            $row->UUnosx = 1;//不是授信的标记
                            return $row;
                        }
                        break;
                    }
                }
                $row->UUtprice = $UUaids_price[$position]?
                    $UUaids_price[$position]:$row->UUtprice;
            }else{                   //应付
                if((int)$row->UUmid == $_SESSION['sid']){//最末级分销商
                    if((int)$row->UUpmode != 2){
                        $row->UUnosx = 1;//不是授信的标记
                    }
                    return $row;
                }
                foreach($aid_money as $v){
                    if($v[0]==$_SESSION['sid']){
                        if($v[1] == 1) //余额
                        {
                            $row->UUnosx = 1;//不是授信的标记
                            return $row;
                        }
                        break;
                    }
                }
                $row->UUtprice = array_search($_SESSION['sid'],$UUaids)?//是否在UUaids里
                    $UUaids_price[array_search($_SESSION['sid'],$UUaids)-1]:
                    $row->UUtprice;
            }
            //不是多级转分销时 直接看支付方式
        }elseif((int)$row->UUpmode != 2){
            $row->UUnosx = 1;//不是授信的标记
        }
        return $row;
    }
}
?>